package com.mini.service.impl;

import cn.hutool.core.date.DateUtil;
import com.mini.domain.entity.MsgSend;
import com.mini.enums.ReceiveStatus;
import com.mini.enums.SendStatus;
import com.mini.exception.ServiceException;
import com.mini.mapper.MsgSendMapper;
import com.mini.service.IMsgSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MsgSendServiceImpl implements IMsgSendService {


    @Autowired
    private MsgSendMapper msgSendMapper;


    @Override
    public int insert(MsgSend msgSend) {

        return msgSendMapper.insert(msgSend);
    }

    @Override
    public Long create(Long userId, List<String> channels) {

        MsgSend msgSend = new MsgSend()
                .setUserId(userId)
                .setChannels(channels.toString())
                .setSendStatus(SendStatus.CREATE)
                .setReceiveStatus(ReceiveStatus.UNKNOWN);

        msgSend.setCreateTime(DateUtil.date());

        int insert = insert(msgSend);

        if (insert == 0) {
            throw new ServiceException("insert msg_send tabel info fail");
        }

        return msgSend.getId();
    }

    @Override
    public void sended(Long id) {
        MsgSend msgSend = msgSendMapper.selectById(id);
        msgSend.setSendStatus(SendStatus.SUCCESS);
        msgSend.setSendTime(DateUtil.date());
        msgSendMapper.updateById(msgSend);
    }
}
